layui.use('form', function(){

    var form = layui.form;
    setTimeout(function (){
        form.render('select');
    }, 500);
    form.on('select(selectRoleId)', function(data){
        vm.afterRoleId = data.value;
    });
});

var vm = new Vue({
    el: '#app',
    data: {
        roleList :{},
        beforeRoleId: "",
        afterRoleId: "",
        userId:"",
        userName:"",
        userTrueName:""
    },
    created() {
        this.getUserDetail()
        this.getAllRole()
    },
    methods: {
        getUserDetail() {
            var userId = location.hash.substring(1)
            $.ajax({
                type: "get",
                url: "/authUser/"+userId,
                headers: { 'Content-Type': 'application/json;charset=utf-8' },
                dataType: 'json',
                async: true,
                data: {},
                success: function (result) {
                    if (result.code == 500) {
                        layer.msg(result.msg)
                    } else {
                        vm.userId= result.data[0].id
                        vm.userName = result.data[0].name
                        vm.userTrueName = result.data[0].userTrueName
                        vm.beforeRoleId = result.data[0].children[0].id
                    }
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    if(xhr.status==403) {
                        alert("无权限")
                    }
                }
            });

        },
        getAllRole() {
            $.ajax({
                type: "get",
                url: "/authRole/list",
                headers: { 'Content-Type': 'application/json;charset=utf-8' },
                dataType: 'json',
                async: true,
                data: {},
                success: function (result) {
                    if (result.code == 500) {
                        layer.msg(result.msg)
                    } else {
                        vm.roleList = result.data
                    }
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    if(xhr.status==403) {
                        alert("无权限")
                    }
                }
            });
        },
        update() {
            var childrenTmp ={
                "id":vm.afterRoleId
            }
            var tmp = {
                "id":vm.userId,
                "children" :[]
            };
            tmp.children.push(childrenTmp)
            var data = JSON.stringify(tmp)
            $.ajax({
                type: "PUT",
                url: "/authUser/userRoleTable",
                headers: { 'Content-Type': 'application/json;charset=utf-8' },
                dataType: 'json',
                async: true,
                data: data,
                success: function (result) {
                    if (result.code == 500) {
                        layer.msg(result.msg)
                    } else {
                        alert("修改成功")
                        window.location.href="roleManger.html"
                    }
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    if(xhr.status==403) {
                        alert("无权限")
                    }
                }
            });
        },
        back(){
            window.location.href="roleManger.html"
        }
    }
})